# coding: utf-8

import socket
from socket import AF_INET
from socket import SOCK_STREAM
import os
from optparse import OptionParser

import config

class Client():
    TEMP = "d:\\temp"
    
    requestURI = "/test/image.html"
    
    def __init__(self):
        pass
    
    def optionHandler(self): # Example: -s localhost:8000 | --serverAddress=localhost:8000
        parser = OptionParser()
        
        (defaultServerHost, defaultServerPort) = config.serverAddress
        parser.set_defaults(SERVER_ADDRESS = defaultServerHost + ":" + str(defaultServerPort)) # default server address
        
        parser.add_option( \
                          "-s", "--serverAddress", dest = "SERVER_ADDRESS", \
                          help = "Set to be connected server address for client" \
                          )
        
        (options, args) = parser.parse_args()
        
        if options.SERVER_ADDRESS.find(":") != -1:
            (optionServerHost, optionServerPort) = options.SERVER_ADDRESS.split(":")
            self.SERVER_ADDRESS = (optionServerHost, int(optionServerPort))
        else:
            self.SERVER_ADDRESS = (options.SERVER_ADDRESS, 80)
        
        #print self.SERVER_ADDRESS
        return True
        
    def requestHandler(self):
        httpRequest = \
            config.clientSupportRequestMethod + " " + self.requestURI + " " + config.clientSupportResponseVersion + config.crlf + \
            "User-Agent:" + config.clientVersion + config.crlf + \
            "Accept-language:en" + config.crlf + config.crlf + \
            ""
            #"Host: neunews.neu.edu.cn" + config.crlf + \
            
        return httpRequest
    
    def responseToRequestAttr(self, dataFromServer):
        dataList = dataFromServer.split(config.crlf, 1)
        dataList2 = dataFromServer.split(config.crlf + config.crlf, 1)
        
        statusLine = dataList[0]
        
        if len(dataList2) == 2:
            responseHeader = dataList2[0]
            responseEntityBody = dataList2[1]
            
            print responseHeader
            
            if statusLine.find("HTTP/") != -1:
                statusLineList = statusLine.split(" ")
                #print statusLineList
                if len(statusLineList) == 3:
                    (httpVersion, statusCode, reasonPhrase) = statusLineList
                else:
                    print "Invalid status line"
                    #return 400
                    
                requestFilePath = self.TEMP + self.requestURI.replace(config.uriSlash, config.pathSlash)
                requestFileFolder = os.path.split(requestFilePath)[0]
                
                if os.path.split(requestFilePath)[1] in ("", "\\"):
                    requestFilePath = requestFileFolder + "\\dynamic.html"
                #print requestFilePath
                #print requestFileFolder
                if not os.path.isdir(requestFileFolder):
                    os.makedirs(requestFileFolder)
                    
                output = open(requestFilePath, "wb")
                output.write(responseEntityBody)
                output.close()
                
                return responseEntityBody
            else:
                print "Unknown response version"
                #return 400
        else:
            return ""
    
    toResquestTags = []
    def startClientSocket(self):
        clientSocket = socket.socket(AF_INET, SOCK_STREAM)
        clientSocket.connect(self.SERVER_ADDRESS)
        
        clientSocket.send(self.requestHandler())
        print "send"
        jumpOutWhile = False
        while True:
            data = clientSocket.recv(102400)
            if not data: break
            
            responseEntityBody = self.responseToRequestAttr(data)
            
            parser = ClientHTMLParser()
            parser.feed(responseEntityBody)
            
            # TODO: depth of folder
            print "parsedTags: ", parser.parsedTags
            if parser.parsedTags:
                while parser.parsedTags:
                    import urlparse
                    temp = parser.parsedTags.pop(0)
                    #print urlparse.urlparse(temp)
                    
                    import string
                    if not urlparse.urlparse(temp).netloc:
                        self.toResquestTags.append([self.SERVER_ADDRESS[0], urlparse.urljoin(self.requestURI, string.strip(temp))])
                    else:
                        self.toResquestTags.append([urlparse.urlparse(temp).netloc, urlparse.urlparse(temp).path])
                    
                    if len(parser.parsedTags) == 0:
                        jumpOutWhile = True
                print "toResquestTags: ", self.toResquestTags
            else:
                jumpOutWhile = True
                print 0
                
            if jumpOutWhile == True:
                break
            
        print 1
        if self.toResquestTags:
            print 2
            while len(self.toResquestTags) != 0:
                print "3 ----------------------"
                toResquestTagsPoped = self.toResquestTags.pop(0)
                
                ##### self.SERVER_ADDRESS
                if toResquestTagsPoped[0] == config.serverAddress[0]:
                    self.SERVER_ADDRESS = config.serverAddress
                else:
                    self.SERVER_ADDRESS = (toResquestTagsPoped[0], 80)
                    
                ##### self.requestURI
                self.requestURI = toResquestTagsPoped[1]
                if not self.requestURI:
                    self.requestURI = config.uriSlash
                
                print "This request server address: ", self.SERVER_ADDRESS
                print "This request request uri: ", self.requestURI
                print "toResquestTags left: ", self.toResquestTags
                print "4 ----------------------"
                clientSocket.close()
                self.startClientSocket()
                
        print "stop"
        
from HTMLParser import HTMLParser

class ClientHTMLParser(HTMLParser):
    parsedTags = []
    
    def handle_starttag(self, tag, attrs):
        if tag in config.htmlTags:
            #print "Tag: " + tag
            #print "Tag to path name: " + config.htmlTags[tag]
            
            for attr in attrs:
                if attr[0] == config.htmlTags[tag]:
                    #print "AttrVal: ", attr[1]
                    self.parsedTags.append(attr[1])
                    
if __name__ == "__main__":
    client = Client()
    if client.optionHandler():
        client.startClientSocket()
    
    """
    import shutil
        if os.path.isdir("d:\\temp\\test"):
            shutil.rmtree("d:\\temp\\test")
    os.startfile("d:\\temp\\test\\image.html")
    """
    
    
    